<?php
class SeatController extends SeatAppController {
	
	public function admin_index($ticket_id = false){
		$this->loadModel('Seat.SeatTicket');
		$seatTicket = $this->SeatTicket->find('first',array(
			'conditions'=>array(
				'SeatTicket.ticket_id'=>$ticket_id
			)
		));
		if(!$seatTicket){
		}
		$this->loadModel('Seat.ModelX');
		$model_id = $seatTicket['SeatTicket']['model_id'];
		$modelxs = $this->ModelX->find('first',array(
			'conditions'=>array('ModelX.id'=>$model_id)
		));
		$seat_ticket_id = $seatTicket['SeatTicket']['id'];
		$this->set(compact('modelxs','seat_ticket_id'));
	}
	
	public function admin_table($table_id,$seat_ticket_id){
		$recursive = 4;
		$this->loadModel('Seat.ModelX');
		$this->ModelX->ModelTable->ModelTr->ModelTd->bindModel(array(
			'hasOne'=>array(
				'Seat'=>array(
					'className' => 'Seat.Seat',
					'foreignKey' => 'td_id',
					'conditions' => array('Seat.seat_ticket_id'=>$seat_ticket_id,'Seat.enable'=>1),
				)
			)
		));
		$table = $this->ModelX->ModelTable->find('first',array(
				'conditions'=>array('ModelTable.id'=>$table_id),
				'recursive'=>$recursive
		));
		$this->set(compact('table'));
	}
	
	public function admin_listSeatType($seat_ticket_id = false){
		$this->loadModel('Seat.SeatType');
		$seatTypes = $this->SeatType->find('all',array(
			'conditions'=>array(
				'SeatType.seat_ticket_id'=>$seat_ticket_id
			)
		));
		$this->set(compact('seatTypes'));
	}
	
	public function admin_addSeatType($seat_ticket_id = false){
		if ($this->request->is(array('post','put'))) {
			$this->loadModel('Seat.SeatType');
			$this->SeatType->create();
			if ($this->SeatType->save($this->request->data)) {
				$this->Session->setFlash(__('The article has been saved.'));
				return $this->redirect(array('action' => 'listSeatType',$seat_ticket_id));
			} else {
				$valid = $this->SeatType->validationErrors;
				$this->Session->setFlash(__('The seat type could not be saved. Please, try again.'));
			}
		}
		$this->set(compact('seat_ticket_id','valid'));
	}
	
	public function admin_editSeatType($id = false){
		$this->loadModel('Seat.SeatType');
		if ($this->request->is(array('post','put'))) {
			$this->SeatType->id = $id;
			if ($this->SeatType->save($this->request->data)) {
				$this->Session->setFlash(__('The article has been saved.'));
				return $this->redirect(array('action' => 'listSeatType',$this->request->data['SeatType']['seat_ticket_id']));
			} else {
				$valid = $this->SeatType->validationErrors;
				$this->Session->setFlash(__('The seat type could not be saved. Please, try again.'));
			}
		}else{
			$this->request->data = $this->SeatType->findById($id);
		}
		$this->set(compact('seat_ticket_id','valid'));
	}

	public function admin_add(){
		if ($this->request->is('post')) {
			$this->Seat->create();
			$check = $this->Seat->find('first',array(
				'conditions'=>array(
					'Seat.td_id'=>$this->request->data['td_id'],
					'Seat.seat_ticket_id'=>$this->request->data['seat_ticket_id'],
				)
			));
			if($check){
				$this->Seat->id = $check['Seat']['id'];
			}
			if ($this->Seat->save($this->request->data)) {
				$state = true;
			}else{
				$state = false;
			}
			$this->set('state',$state);
		}
	    $this->render(false);
	}
	
	public function admin_edit($id){
		if ($this->request->is('post')) {
			$this->Seat->id = $id;
			if ($this->Seat->save($this->request->data)) {
				$state = true;
			}else{
				$state = false;
			}
			$this->set('state',$state);
		}
		$this->render(false);
	}
	
	public function admin_del($id){
		if ($this->request->is('post')) {
			$this->Seat->id = $id;
			if ($this->Seat->saveField('enable',0)) {
				$state = true;
			}else{
				$state = false;
			}
			$this->set('state',$state);
		}
		$this->render(false);
	}
}